package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import vo.Baoyang;
import vo.Cus;

public class BaoyangDao {
	Connection conn=null;
	PreparedStatement ps=null;
	Statement st=null;
	ResultSet rs=null;	
	String url = "jdbc:mysql://localhost:3306/baoyang?useUnicode=true&characterEncoding=utf8"; 
    String user = "root"; 
    String password = "123456"; 
		
	Connection getConnection(){
		try {
		    conn = DriverManager.getConnection(url, user, password);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}
	
	void close(Connection conn){
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	void close(Connection conn,Statement ps){
		try {
			ps.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.close(conn);
	}
	
	void close(Connection conn,Statement ps,ResultSet rs){
		try {
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.close(conn, ps);
	}
	
	static{
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public List<Baoyang> getAll(String no){
		List<Baoyang> list=new ArrayList<Baoyang>();
		this.getConnection();
		try {
			if(no==null)
				no="";
			String sql="select * from baoyang where no like'%"+no+"%'";
			ps=conn.prepareStatement(sql);
			rs=ps.executeQuery();
			while(rs.next()){
				Baoyang a=new Baoyang();
				a.setId(rs.getInt("id"));
				a.setNo(rs.getString("no"));
				a.setShijian(rs.getString("shijian"));
				a.setDetail(rs.getString("detail"));
				
				list.add(a);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return list;
	}	
	
	public List<Baoyang> getMess(int id){
		List<Baoyang> list=new ArrayList<Baoyang>();
		this.getConnection();
		try {

			String sql="select * from baoyang where id ="+id;
			ps=conn.prepareStatement(sql);
			rs=ps.executeQuery();
			while(rs.next()){
				Baoyang a=new Baoyang();
				a.setId(rs.getInt("id"));
				a.setNo(rs.getString("no"));
				a.setShijian(rs.getString("shijian"));
				a.setDetail(rs.getString("detail"));
				
				list.add(a);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return list;
	}	
		
	public void insert(String no,String shijian,String detail){
		this.getConnection();
		try {
			ps= conn.prepareStatement("insert into Baoyang(no,shijian,detail) values(?,?,?)");

			ps.setString(1, no);
			ps.setString(2, shijian);
			ps.setString(3, detail);
			
			ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			this.close(conn, ps);
		}
	}
	
	
	public void delete(int id){
		this.getConnection();
		try {
			ps=conn.prepareStatement("delete from Baoyang where id="+id);
			ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			this.close(conn, ps);
		}		
	}
	
	public void update(int id,String no,String shijian,String detail){
		this.getConnection();
		try {
			ps=conn.prepareStatement("update baoyang set no='"+no+"' ,shijian='"+shijian+"',detail='"+detail+"' where id="+id);
			ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			this.close(conn, ps);
		}		
	}

}
